﻿<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="BlogRead.aspx.cs" Inherits="TimeCapsule.BlogRead" MasterPageFile="~/MasterPage.Master" EnableEventValidation="false" ValidateRequest="false"%>

<asp:Content ID="Content1" ContentPlaceHolderID="Head" runat="Server">

<link type="text/css" href="common/blitzer/jquery-ui-1.8.16.custom.css" rel="Stylesheet" />
<script type="text/javascript" src="lib/prototype.js"></script>
<script type="text/javascript" src="lib/jquery-1.7.min.js"></script>
<script type="text/javascript" src="lib/jquery-ui-1.8.16.custom.min.js"></script>

<script type="text/javascript">
	var _Active;
	var deleteConfirmed = false;
	$j = jQuery.noConflict();

	$j(document).ready(function() {
		$j('#dlgDeleteReply').dialog({
			autoOpen: false,
			modal: true,
			bgiframe: true,
			title: "댓글 삭제",
			width: 300,
			height: 150
		});

		$j('#dlgDeleteReplyMember').dialog({
			autoOpen: false,
			modal: true,
			bgiframe: true,
			title: "댓글 삭제",
			width: 300,
			height: 120
		});

		$j('#dlgErrorWrongPassword').dialog({
			autoOpen: false,
			modal: true,
			bgiframe: true,
			title: "비밀번호 오류",
			width: 300,
			height: 120
		});

		$j('#dlgErrorWrongAccess').dialog({
			autoOpen: false,
			modal: true,
			bgiframe: true,
			title: "접근 오류",
			width: 300,
			height: 120
		})

		$j("#divAcc").accordion({
			active: false,
			collapsible: true,
			autoHeight: false,
			change: function(event, ui) {
				$('hdnCurrent').value = ui.options.active;
				if (ui.newContent.length > 0) {
					$('ActivePostIdx').value = ui.newContent.children().attr("id").replace("divPost_", "");
				}
				else
					$j('ActivePostIdx').value = "-1";

			}
		});

		$('hdnCurrent').value = getParam("cur") == "" ? "-1" : getParam("cur");
		if ($('hdnCurrent').value != -1) {
			$j("#divAcc").accordion("activate", parseInt($('hdnCurrent').value));
		}

		AddUi();
	});

	function AddUi() {
		$j(".btnJQ").button();

		$j(".btnJQdelete").button({
			icons: {
				primary: "ui-icon-closethick"
			}
		});
		$j(".btnJQdelete").css("padding-left", 5).css("padding-top", 0).css("width", "15px").css("height", "20px");
	}

	function getParam(name) {
		name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
		var regexS = "[\\?&]" + name + "=([^&#]*)";
		var regex = new RegExp(regexS);
		var results = regex.exec(window.location.href);
		if (results == null)
			return "";
		else
			return results[1];
	}

	function DeleteReplyPopup(idx, member, obj) {
		if(!deleteConfirmed)
		{
			$('hdnReplyIdx').value = idx;
			if (member) {
				$j('#dlgDeleteReplyMember').dialog('option', 'buttons',
				{
					"OK": function() {
						$j(this).dialog("close");
						deleteConfirmed = true
						$('txtReplyPassword').value = "";
						obj.click();
					},
					"Close": function() { $j(this).dialog("close"); $('txtReplyPassword').value = ""; }
				})
				$j('#dlgDeleteReplyMember').dialog('open');
			}
			else {
				$j('#dlgDeleteReply').dialog('option', 'buttons',
				{
					"OK": function() {
						$('hdnReplyPassword').value = $('txtReplyPassword').value;
						$j(this).dialog("close");
						deleteConfirmed = true;
						$('txtReplyPassword').value = "";
						obj.click();
					},
					"Close": function() { $j(this).dialog("close"); $('txtReplyPassword').value = ""; }
				})
				$j('#dlgDeleteReply').dialog('open');
			}
		}
		
		return deleteConfirmed;
	}

	function OpenErrorPopup(id) {
		$j('#' + id ).dialog('option', 'buttons',
		{
			"OK": function () {	$j(this).dialog("close"); }
		})
		$j('#' + id).dialog('open');
	}

	function ValidateWriteReply(s) {
		var id = s.parentElement.parentElement.getElementsByClassName("cssReplyId")[0];
		var body = s.parentElement.parentElement.getElementsByClassName("cssReplyBody")[0];
		var psw = s.parentElement.parentElement.getElementsByClassName("cssReplyPsw")[0];
		if (id != null && id.value == "") {
			alert("이름를 입력하세요.");
			return false;
		}
		if(body != null && body.value == "") {
			alert("내용을 입력하세요.");
			return false;
		}
		if (psw != null && psw.value == "") {
			alert("비밀번호를 입력하세요.");
			return false;
		}
		if(psw != null && psw.value.length < 4) {
			alert("비밀번호는 4자 이상 입력하세요.");
			return false;
		}
		return true;
	}
	/*
	function DeleteReply(member) {
		new Ajax.Request("lib/dbAccess.ashx", {
			parameters: {
				type: "deleteReply",
				idx: $('hdnReplyIdx').value,
				psw: member ? "" : $('txtReplyPassword').value
			},
			asynchronous: false,
			evalJS: true,
			on200: function(r) {
				var res = r.responseJSON;
				if (res.valid) {
					//location.reload();
					location.replace("BlogRead.aspx?page=" + getParam("page") + "&cur=" + $('hdnCurrent').value);
				}
				else {
					alert(res.msg);
				}
			},
			on500: function(res) {
				alert("오류가 발생했습니다.");
			}
		});

		$('hdnReplyIdx').value = "";
		if (member) {
			$j('#dlgDeleteReplyMember').dialog('close');
		}
		else {
			$('txtReplyPassword').value = "";
			$j('#dlgDeleteReply').dialog('close');
		}
	}
	*/
	function KeyPressed(s, e) {
		if (e.keyCode == 13) {
			if (s.value == "")
				return false;
			if (s.getAttribute("class") == "cssReplyId") {
				s.parentElement.parentElement.getElementsByClassName("cssReplyBody")[0].focus();
				return false;
			}
			else if (s.getAttribute("class") == "cssReplyBody") {
				s.parentElement.parentElement.getElementsByClassName("cssReplyPsw")[0].focus();
				return false;
			}
			else if (s.getAttribute("class") == "cssReplyPsw") {
			if (ValidateWriteReply(s))
				$(s.parentElement.parentElement.getElementsByClassName("cssReplySubmit")[0].getAttribute("id")).click();
				return false;
			}
		}
	}
	
</script>

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="Body" runat="server">
	<asp:ScriptManager runat="Server" />
	<div id="divAdminWrite" runat="Server" visible="false">
		<p style="text-align:right;">
			<a href="BlogWrite.aspx" class="btnJQ">글쓰기</a>
		</p>
	</div>
	<div class="divBlogRead">
		<div id="divAcc">
			<asp:Repeater ID="dlBlog" runat="Server">
				<ItemTemplate>
					<h3>
						<a href="#">
							<table class="blogList">
								<tr class="row_head">
									<td class="col_idx">
										<%# Eval("Idx") %>
									</td>
									<td class="col_title">
										<%# Eval("Title") %>
									</td>
									<td class="col_date">
										<%# TimeCapsule.Util.DateFormatChange(Eval("Date").ToString(), "yy-mm-dd") %>
									</td>
								</tr>
							</table>
						</a>
					</h3>
					<div>
						<div id='divPost_<%# Eval("Idx") %>' class="divBlogBody">
							<div class="divBlogBodyContent">
								<%# Eval("Body") %>
							</div>
							<div class="divBlogReply">
								<script type="text/javascript">
									var prm = Sys.WebForms.PageRequestManager.getInstance();
									prm.add_endRequest(function() {
										AddUi();
										deleteConfirmed = false;
									});
								</script>
								<asp:UpdatePanel id="udp" runat="Server" UpdateMode="Always">
									<ContentTemplate>
										<asp:Repeater Id="dlBlogReply" runat="Server" OnItemCommand="btnWriteRpl_Clicked">
											<ItemTemplate>
												<table class="writeReply2">
													<tr class="trWriteReply">
														<td class="id">
															<font color="red"><%# Eval("Id") %></font>
														</td>
														<td class="body">
															<font color="red"><%# Eval("Body") %></font>
														</td>
														<td class="date">
															<font color="red"><%# TimeCapsule.Util.DateFormatChange(Eval("Date").ToString(), "yy.mm.dd") %></font>
														</td>
														<td class="button">
															<%--회원용--%>
															<%--<asp:Button ID="Button1" runat="Server" CommandName="DeleteReply" OnClientClick='<%# "javascript:DeleteReplyPopup(" + Eval("idx") + ", true); return false;" %>' Text="x" CssClass="btnJQdelete"
																	Visible='<%# mb.Role == TimeCapsule.Role.admin || ( mb.key != "" && mb.Idx.ToString() == Eval("LoginIdx").ToString() ) ? true : false %>' /> --%>
															<asp:Button ID="Button2" runat="Server" CommandName="DeleteReply" OnClientClick='<%# "javascript:return DeleteReplyPopup(" + Eval("idx") + ", true, this);" %>' Text="x" CssClass="btnJQdelete"
																	Visible='<%# mb.Role == TimeCapsule.Role.admin || ( mb.key != "" && mb.Idx.ToString() == Eval("LoginIdx").ToString() ) ? true : false %>' /> 

															<%--비회원용--%>
															<%--<asp:Button ID="Button2" runat="Server" CommandName="DeleteReply" OnClientClick='<%# "javascript:DeleteReplyPopup(" + Eval("idx") + ", false); return false;" %>' Text="x" CssClass="btnJQdelete"
																	 Visible='<%# mb.key == "" && Eval("LoginIdx").ToString() == "-1" ? true : false %>' />--%>
															<asp:Button ID="Button4" runat="Server" CommandName="DeleteReply" OnClientClick='<%# "javascript:return DeleteReplyPopup(" + Eval("idx") + ", false, this);" %>' Text="x" CssClass="btnJQdelete"
																	 Visible='<%# mb.key == "" && Eval("LoginIdx").ToString() == "-1" ? true : false %>' />
														</td>
													</tr>
												</table>
											</ItemTemplate>
											<FooterTemplate>
												<table class="writeReply">
													<tr class="trWriteReply">
														<td class="name">
															이름
														</td>
														<td class="body">
															내용
														</td>
														<td class="password" runat="Server" id="tdPasswordProperty" visible='<%# mb.key == "" ? true : false %>'>
															비밀번호
														</td>
													</tr>
													<tr class="trWriteReply2">
														<td class="id">
															<asp:TextBox ID="txtReplyId" runat="Server" MaxLength="16" CssClass="cssReplyId" Width="95%" Visible='<%# mb.key == "" ? true : false %>' Text="" onkeypress="return KeyPressed(this, event);" />
															<asp:Label ID="lblReplyId" runat="Server" Width="100%" Visible='<%# mb.key == "" ? false : true %>' Text='<%# mb.key == "" ? "" : mb.Name %>' />
														</td>
														<td class="body">
															<asp:TextBox id="txtReplyBody" runat="server" CssClass="cssReplyBody" Width="98%" onkeypress="return KeyPressed(this, event);" />
														</td>
														<td class="password" runat="Server" id="tdPasswordValue" Visible='<%# mb.key == "" ? true : false %>'>
															<asp:TextBox ID="txtReplyPsw" runat="Server" TextMode="Password" CssClass="cssReplyPsw" MaxLength="16" Width="95%" onkeypress="return KeyPressed(this, event);" />
														</td>
														<td class="btnWrite">
															<asp:Button id="btnWriteRpl" runat="Server" OnClientClick="javascript:return ValidateWriteReply(this); return false;" CommandName="WriteReply" CssClass="cssReplySubmit btnJQ" Text="쓰기" />
														</td>
													</tr>
												</table>	
											</FooterTemplate>
										</asp:Repeater>
									</ContentTemplate>
								</asp:UpdatePanel>
							</div>
						</div>
					</div>
				</ItemTemplate>
			</asp:Repeater>
		</div>
		
	</div>
	
	
	<div class="blogReadButton">
		<asp:Button ID="btnPrev" runat="Server" OnClick="PrevPage" CssClass="btnJQ" Text="이전"/>
		<asp:Button ID="btnNext" runat="Server" OnClick="NextPage" CssClass="btnJQ" Text="다음"/>
	</div>
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="Dialog" runat="Server">
	<div id="dlgDeleteReply" style="display:none;" class="commonDialog">
		<div class="commonDialog">
			<h1>삭제하시겠습니까?</h1>
			<h2>
			비밀번호 : 
			<input type="password" id="txtReplyPassword" name="txtReplyPassword" />
			</h2>
		</div>
	</div>

	<div id="dlgDeleteReplyMember" style="display:none;" class="commonDialog">
		<div class="commonDialog">
			<h1>삭제하시겠습니까?</h1>
		</div>	
	</div>
	
	<div id="dlgErrorWrongPassword" style="display:none;" class="commonDialog">
		<div class="commonDialog">
			<h2>비밀번호를 잘못 입력하셨습니다.</h2>
		</div>
	</div>
	
	<div id="dlgErrorWrongAccess" style="display:none;" class="commonDialog">
		<div class="commonDialog">
			<h2>올바른 접근이 아닙니다.</h2>
		</div>
	</div>
</asp:Content>